import * as Constant from '../../utils/constant'
import sys_manage from '@/api/sys_manage'
// initial state
const state = {
  isValids: [],
  processTypes: [],
  systemTypes: [],
  subjectDepts: [],
  currencyList: []
}

// getters
const getters = {
  isValidMap: state => {
    var obj = {}
    state.isValids.map(function (item) { obj[item.dictKey] = item.dictText })
    return obj
  },
  processTypeMap: state => {
    var obj = {}
    state.processTypes.map(function (item) { obj[item.dictKey] = item.dictText })
    return obj
  },
  systemTypeMap: state => {
    var obj = {}
    state.systemTypes.map(function (item) { obj[item.dictKey] = item.dictText })
    return obj
  },
  subjectDeptMap: state => {
    var obj = {}
    state.subjectDepts.map(function (item) { obj[item.dictKey] = item.dictText })
    return obj
  },
  currencyMap: state => {
    var obj = {}
    state.currencyList.map(function (item) { obj[item.dictKey] = item.dictText })
    return obj
  }
}

// actions
const actions = {
  sysDictLoadByPaging ({ commit }) {
    let isValids = []
    let processTypes = []
    let systemTypes = []
    let subjectDepts = []
    let currencyList = []
    sys_manage.sysDictLoadByPaging({dictGroupKey: 'isValid'}, result => {
      if (result.code === Constant.RT_SUCCESS) {
        isValids = result.data
      }
      commit('setIsValids', isValids)
    })
    sys_manage.sysDictLoadByPaging({dictGroupKey: 'ProcessType'}, result => {
      if (result.code === Constant.RT_SUCCESS) {
        processTypes = result.data
      }
      commit('setProcessTypes', processTypes)
    })
    sys_manage.sysDictLoadByPaging({dictGroupKey: 'systemType'}, result => {
      if (result.code === Constant.RT_SUCCESS) {
        systemTypes = result.data
      }
      commit('setSystemTypes', systemTypes)
    })
    sys_manage.sysDictLoadByPaging({dictGroupKey: 'achievement_profession_dept'}, result => {
      if (result.code === Constant.RT_SUCCESS) {
        subjectDepts = result.data
      }
      commit('setSubjectDepts', subjectDepts)
    })
    sys_manage.sysDictLoadByPaging({dictGroupKey: 'run_currency'}, result => {
      if (result.code === Constant.RT_SUCCESS) {
        currencyList = result.data
      }
      commit('setSurrencyList', currencyList)
    })
  }
}

// mutations
const mutations = {
  setIsValids (state, result) {
    state.isValids = result
  },
  setProcessTypes (state, result) {
    state.processTypes = result
  },
  setSystemTypes (state, result) {
    state.systemTypes = result
  },
  setSubjectDepts (state, result) {
    state.subjectDepts = result
  },
  setSurrencyList (state, result) {
    state.currencyList = result
  }
}

export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations
}